package com.itlong.cloud.controller.finance;

import com.itlong.cloud.POJO.DTO.operate.OperateAssignmentManageDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateAssignmentDetailVO;
import com.itlong.cloud.POJO.VO.operate.OperateAssignmentPageVO;
import com.itlong.cloud.annotation.UnAuthAccessRequired;
import com.itlong.cloud.annotation.UnParamEncrypt;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.paymenthub.IOperateAssignmentManageServiceClient;
import com.itlong.cloud.project.operate.IOperateU8BaseDataServiceClient;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <desc>
 *      云服务费任务书管理控制器
 * </desc>
 *
 * @createDate2020/11/30
 **/
@RestController
@RequestMapping("/finance/assignment")
public class OperateAssignmentManageController {


    @Autowired
    private IOperateAssignmentManageServiceClient iOperateAssignmentManageServiceClient;

    @Autowired
    private IOperateU8BaseDataServiceClient iOperateU8BaseDataServiceClient;

    @Autowired
    private RedisService redisService;

    //同步任务书线程池
    private static ExecutorService assignmentSyncThreadPool = Executors.newFixedThreadPool(10);


    /**
     * <desc>
     *      获取任务书详情
     * </desc>
     *
     * @param operateAssignmentManageDTO 任务书管理参数DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/11/30
     */
    @PostMapping("/getDetail")
    public Object getDetail(OperateAssignmentManageDTO operateAssignmentManageDTO, HttpServletRequest request){
        try{
            operateAssignmentManageDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateAssignmentManageDTO.class, request);
            if (operateAssignmentManageDTO == null || StringUtils.isBlank(operateAssignmentManageDTO.getAssignmentNumber())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            OperateAssignmentDetailVO assignmentDetailVO = iOperateAssignmentManageServiceClient.getDetail(operateAssignmentManageDTO);
            if (assignmentDetailVO == null){
                return new MessageVO(OperateErrorCodeEnum.OPERATE_ASSIGNMENT_NOT_EXIST_ERR.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), assignmentDetailVO);
        }catch (Exception e){
            throw new DataAccessException("【运营_任务书管理getDetail】获取任务书管理详情失败", e);
        }
    }

    /**
     * <desc>
     *      分页获取任务书列表
     * </desc>
     *
     * @param operateAssignmentManageDTO 运营任务书管理DTO
     * @return 任务书分页数据
     * @Author Zhu.yj
     * @createDate  2020/12/1
     */
    @PostMapping("/getPage")
    public Object getPage(OperateAssignmentManageDTO operateAssignmentManageDTO, HttpServletRequest request){
        try {
            operateAssignmentManageDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateAssignmentManageDTO.class, request);
            if (operateAssignmentManageDTO == null){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<OperateAssignmentPageVO> page = iOperateAssignmentManageServiceClient.getPage(operateAssignmentManageDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), page);
        }catch (Exception e){
            throw new DataAccessException("【运营_任务书管理getPage】获取任务数管理分页失败", e);
        }
    }

    /**
     * <desc>
     *      手动同步任务书
     * </desc>
     *
     * @return 格式化响应参数
     * @Author Zhu.yj
     * @createDate  2020/12/17
     */
    @RequestMapping(value = "/assignmentSync", method = RequestMethod.POST)
    public Object assignmentSync( HttpServletRequest request){
        try{
            OperateAssignmentManageDTO operateAssignmentManageDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateAssignmentManageDTO.class, request);
            if (operateAssignmentManageDTO == null || StringUtils.isBlank(operateAssignmentManageDTO.getAssignmentNumber())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if(redisService.get(RedisConstant.ASSIGNMENT_SYNC_REDIS_KEY + "*") != null){
                return new MessageVO(OperateErrorCodeEnum.OPERATE_ASSIGNMENT_CURRENT_SYNC_ERR.getErrorCode());
            }
            iOperateAssignmentManageServiceClient.assignmentSync(operateAssignmentManageDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_任务书管理assignmentSync】手动同步任务书失败", e);
        }
    }

    /**
     * <desc>
     *      获取任务书同步百分比
     * </desc>
     *
     * @return
     * @Author Zhu.yj
     * @createDate  2020/12/30
     */
    @PostMapping("/getPercent")
    public Object getPercent(HttpServletRequest request){
        try{
            String myUserName = request.getAttribute("myUserName") == null ? null : request.getAttribute("myUserName").toString();
            if (StringUtils.isBlank(myUserName)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            String percent = redisService.get(RedisConstant.ASSIGNMENT_SYNC_REDIS_KEY + myUserName);
            //同步进度到百分百时清除缓存
            if("1.0".equals(percent)){
                redisService.del(RedisConstant.ASSIGNMENT_SYNC_REDIS_KEY + myUserName);
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), (Object)percent);
        }catch (Exception e){
            throw new DataAccessException("【运营_任务书管理getPercent】获取任务书同步百分比失败", e);
        }
    }

    /**
     * <desc>
     *      自动创建首三年云服务费订单接口(手动调用定时任务)
     * </desc>
     *
     * @param request
     * @return
     * @Author Zhu.yj
     * @createDate  2021/3/1
     */
    @UnParamEncrypt
    @UnAuthAccessRequired
    @PostMapping("/autoCreate")
    public Object autoCreate(HttpServletRequest request, String firstSync){
        try {
            if (firstSync ==  null){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //自动创建云服务费
            iOperateAssignmentManageServiceClient.createCloudServiceCostOrderTimeTask(firstSync);
            //U8数据同步
//            iOperateU8BaseDataServiceClient.timeTaskSynchronizeBaseDataByU8();
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【自动创建首三年云服务费订单失败】", e);
        }
    }

}